package com.zhht.htzx.utils;

import com.zhht.htzx.model.Order;
import com.zhht.htzx.model.OrderItem;
import com.zhht.htzx.model.Order.OrderStatus;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * 测试数据生成器
 */
public class TestDataGenerator {
    
    private static final String[] PRODUCT_NAMES = {
        "iPhone 15 Pro Max", "MacBook Pro 14", "iPad Air", "Apple Watch Series 9",
        "AirPods Pro", "iMac 24", "Mac mini", "Apple TV 4K", "HomePod mini", "Magic Keyboard"
    };
    
    private static final String[] PRODUCT_SPECS = {
        "256GB 深空黑色", "512GB 银色", "128GB 蓝色", "256GB 午夜色",
        "白色", "绿色", "粉色", "黄色", "紫色", "橙色"
    };
    
    private static final String[] TRACKING_COMPANIES = {
        "顺丰快递", "圆通快递", "中通快递", "申通快递", "韵达快递", "EMS"
    };
    
    private static final Random random = new Random();
    
    /**
     * 生成测试订单数据
     */
    public static List<Order> generateTestOrders(String userId, int count) {
        List<Order> orders = new ArrayList<>();
        
        for (int i = 0; i < count; i++) {
            Order order = generateRandomOrder(userId, i + 1);
            orders.add(order);
        }
        
        return orders;
    }
    
    /**
     * 生成随机订单
     */
    private static Order generateRandomOrder(String userId, int index) {
        // 生成订单ID
        String orderId = "ORDER" + System.currentTimeMillis() + "_" + index;
        
        // 生成商品列表
        List<OrderItem> items = generateRandomOrderItems();
        
        // 计算总金额
        double totalAmount = 0;
        for (OrderItem item : items) {
            totalAmount += item.getTotalPrice();
        }
        
        // 创建订单
        Order order = new Order(orderId, userId, items, totalAmount);
        
        // 随机设置订单状态
        OrderStatus[] statuses = OrderStatus.values();
        OrderStatus randomStatus = statuses[random.nextInt(statuses.length)];
        order.setStatus(randomStatus);
        
        // 设置收货信息
        order.setReceiverName("张三" + index);
        order.setReceiverPhone("138" + String.format("%08d", random.nextInt(100000000)));
        order.setAddress("北京市朝阳区xxx街道xxx号" + index + "室");
        
        // 设置备注
        if (random.nextBoolean()) {
            order.setRemark("请尽快发货，谢谢！");
        }
        
        // 设置优惠金额
        if (random.nextBoolean()) {
            double discount = totalAmount * 0.1; // 10%优惠
            order.setDiscountAmount(discount);
            order.setActualAmount(totalAmount - discount);
        }
        
        // 设置物流信息（已发货状态）
        if (randomStatus == OrderStatus.SHIPPED || randomStatus == OrderStatus.DELIVERED || 
            randomStatus == OrderStatus.COMPLETED) {
            order.setTrackingCompany(TRACKING_COMPANIES[random.nextInt(TRACKING_COMPANIES.length)]);
            order.setTrackingNumber("SF" + String.format("%010d", random.nextInt(1000000000)));
        }
        
        // 设置时间
        Date createTime = new Date(System.currentTimeMillis() - random.nextInt(30) * 24 * 60 * 60 * 1000L);
        order.setCreateTime(createTime);
        
        // 根据状态设置相应时间
        switch (randomStatus) {
            case PAID:
            case SHIPPED:
            case DELIVERED:
            case COMPLETED:
            case REFUNDING:
            case REFUNDED:
                order.setPayTime(new Date(createTime.getTime() + 2 * 60 * 60 * 1000L));
                break;
        }
        
        switch (randomStatus) {
            case SHIPPED:
            case DELIVERED:
            case COMPLETED:
                order.setShipTime(new Date(createTime.getTime() + 24 * 60 * 60 * 1000L));
                break;
        }
        
        switch (randomStatus) {
            case DELIVERED:
            case COMPLETED:
                order.setDeliveryTime(new Date(createTime.getTime() + 3 * 24 * 60 * 60 * 1000L));
                break;
        }
        
        return order;
    }
    
    /**
     * 生成随机订单商品
     */
    private static List<OrderItem> generateRandomOrderItems() {
        List<OrderItem> items = new ArrayList<>();
        
        // 随机生成1-3个商品
        int itemCount = random.nextInt(3) + 1;
        
        for (int i = 0; i < itemCount; i++) {
            String productName = PRODUCT_NAMES[random.nextInt(PRODUCT_NAMES.length)];
            String spec = PRODUCT_SPECS[random.nextInt(PRODUCT_SPECS.length)];
            double price = 100 + random.nextDouble() * 9000; // 100-9100元
            int quantity = random.nextInt(3) + 1; // 1-3件
            
            OrderItem item = new OrderItem(
                "PROD" + System.currentTimeMillis() + "_" + i,
                productName,
                "https://example.com/product" + i + ".jpg",
                spec,
                price,
                quantity
            );
            
            items.add(item);
        }
        
        return items;
    }
    
    /**
     * 初始化测试数据
     */
    public static void initializeTestData(OrderManager orderManager, String userId) {
        // 清空现有数据
        orderManager.clearAllOrders();
        
        // 生成测试订单
        List<Order> testOrders = generateTestOrders(userId, 10);
        
        // 保存到本地
        for (Order order : testOrders) {
            orderManager.saveOrder(order);
        }
    }
} 